package courseManagebl;

import java.rmi.RemoteException;
import java.util.ArrayList;

import message.Message;
import courseManageDataService.CourseManageDataService;
import dataFactory.DataFactory;
import PO.CoursePO;
import PO.UserPO;
import VO.CourseVO;
import VO.UserVO;

public class CourseManage {
	
	public Message publishCourse(CourseVO vo) throws RemoteException {
		DataFactory factory=new DataFactory();
		CourseManageDataService cmds=factory.getCourseManageDataService();
		CoursePO coursePO=new CoursePO(vo.getDepart(),vo.getCourseNO(),vo.getCourseName(),
				vo.getCourseType(),vo.getCredit(),vo.getCourseNO(),vo.getTime(),vo.getClassroom(),vo.getTeacher());
		
		return cmds.newCourse(coursePO);
	}
	
	public Message complete_class_inform(CourseVO vo) throws RemoteException{
		DataFactory factory=new DataFactory();
		CourseManageDataService cmds=factory.getCourseManageDataService();
		CoursePO coursePO=new CoursePO(vo.getDepart(),vo.getCourseNO(),vo.getCourseName(),
				vo.getCourseType(),vo.getCredit(),vo.getCourseNO(),vo.getTime(),vo.getClassroom(),
				vo.getTeacher());
		coursePO.setDescibtion(vo.getDescribtion());
		return cmds.newCourse(coursePO);
	}
	
	
	public ArrayList<CourseVO> showCourse(UserVO uservo) throws RemoteException{
		DataFactory factory=new DataFactory();
		CourseManageDataService cmds=factory.getCourseManageDataService();
		
		UserPO userPO=new UserPO(uservo.getId(),uservo.getCode(),uservo.getName(),uservo.getRole());
		
		ArrayList<CoursePO> poList=cmds.find(userPO);
		ArrayList<CourseVO> voList=new ArrayList<CourseVO>();
		for(int i=0;i<poList.size();i++){
			CoursePO po=poList.get(i);
			CourseVO vo=new CourseVO(po.getDepart(),po.getCourseNO(),po.getCourseName(),
					po.getCourseType(),po.getCredit(),po.getCourseNO(),po.getTime(),po.getClassroom(),
					po.getTeacher());
		}
		return voList;
		
	}
	public ArrayList<CourseVO> showCourse(String requirement) throws RemoteException{
		DataFactory factory=new DataFactory();
		CourseManageDataService cmds=factory.getCourseManageDataService();
		ArrayList<CoursePO> poList=cmds.find(requirement);
		ArrayList<CourseVO> voList=new ArrayList<CourseVO>();
		for(int i=0;i<poList.size();i++){
			CoursePO po=poList.get(i);
			CourseVO vo=new CourseVO(po.getDepart(),po.getCourseNO(),po.getCourseName(),
					po.getCourseType(),po.getCredit(),po.getCourseNO(),po.getTime(),po.getClassroom(),
					po.getTeacher());
		}
		return voList;
		
	}
	
	public Message comment(CourseVO courseVO, int comment) throws RemoteException{
		//initialization
		DataFactory factory=new DataFactory();
		CourseManageDataService cmds=factory.getCourseManageDataService();
		//reset comment
		int[] bComment=courseVO.getComment();
		bComment[0]=(bComment[0]+comment)/(bComment[1]+1);
		bComment[1]++;
		courseVO.setComment(bComment);
		//create coursePO
		CoursePO coursePO=new CoursePO(courseVO.getDepart(),courseVO.getCourseNO(),courseVO.getCourseName(),
				courseVO.getCourseType(),courseVO.getCredit(),courseVO.getCourseNO(),courseVO.getTime(),
				courseVO.getClassroom(),courseVO.getTeacher());
		coursePO.setDescibtion(courseVO.getDescribtion());
		coursePO.setComment(courseVO.getComment());
		//call update Function
		return cmds.update(coursePO);
	}

	public ArrayList<String> CountBydp() {
		// TODO Auto-generated method stub
		return null;
	}

}
